<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Send Notification (Google Chat)
 * @description : Send a chat message to users for notifying new tweets, replies, polls, etc..
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Send_notification extends AUTH_Controller {

  var $send_chat_to = 'HOME';

  public function __construct() {
    parent::__construct();
    $this->send_chat_to = $this->config->item('send_chat_to');
  }

  /**
   * Initilize notification sending preparation
   * 
   */
  public function init($notify_type = NULL, $notify_type_id = NULL, $notify_type_hash = NULL) {

    //@TODO Disable This
    return;

    // Notifying data
    $notify_data = array();

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Getting notification data
      if (!empty($notify_type) && !empty($notify_type_id) && !empty($notify_type_hash)) {
        if ($notify_type == 'new_tweet') {
          $notify_data = $this->for_new_tweet($notify_type_id, $notify_type_hash);
        } elseif ($notify_type == 'new_reply') {
          $notify_data = $this->for_new_reply($notify_type_id, $notify_type_hash);
        } elseif ($notify_type == 'new_poll') {
          $notify_data = $this->for_new_poll($notify_type_id, $notify_type_hash);
        } elseif ($notify_type == 'new_album') {
          $notify_data = $this->for_new_album($notify_type_id, $notify_type_hash);
        } elseif ($notify_type == 'new_event') {
          $notify_data = $this->for_new_event($notify_type_id, $notify_type_hash);
        } elseif ($notify_type == 'new_wall_post') {
          $notify_data = $this->for_new_wall_post($notify_type_id, $notify_type_hash);
        } else {
          $notify_data = array();
        }
      }

      // Preparing thread chat notification command
      if (!empty($notify_data['email_ids']) && !empty($notify_data['email_msg']) && !empty($notify_data['email_hash'])) {

        // Setting chat notification sending controller
        $index_php = FCPATH . 'index.php';
        $controller = 'background_process';
        $method = 'send_chat';

        // Init variables
        $count = 0;
        $command = NULL;

        // Preparing chat emails ids
        $email_ids = @array_unique(@array_filter($notify_data['email_ids']));

        // Checking email id & security hash
        if (!empty($email_ids) && is_array($email_ids) && ($notify_type_hash == $notify_data['email_hash'])) {

          // Preparing command for each email id
          foreach ($email_ids as $email_id) {
            $param1 = preg_replace("/[^a-zA-Z0-9]+/", "", base64_encode($email_id));
            $param2 = preg_replace("/[^a-zA-Z0-9]+/", "", base64_encode($notify_data['email_msg']));
            $command .= 'php' . ' ' . $index_php . ' ' . $controller . ' ' . $method . ' ' . $param1 . ' ' . $param2 . " & \n";
            $count++;
          }

          // Thread execution (Special Thanks Vineeth P.M)
          system($command);
        }

        // Returing success response
        echo $count . ' Notifications Sent';
        return;
      }

      // Returning error response
      echo 'No notifications sent';
      return;
    }

    // Redirect if invalid request
    redirect();
  }

  /**
   * Preparing notification data for new reply
   * 
   */
  private function for_new_reply($tweet_id = NULL, $tweet_hash = NULL) {

    // Preparing return data
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;

    $email_ids = array();

    // Checking tweet id & tweet hash
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($tweet_hash)) {

      // Getting tweet details, followers, etc
      $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);
      $get_followers = $this->tweet_model->get_followers($tweet_id);
      $get_tweet_reply_by_id = $this->tweet_model->get_tweet_reply_by_id($tweet_id, 1);

      // Checking tweet
      if (!empty($get_tweet_by_id['result']) && !empty($get_followers['result']) && !empty($get_tweet_reply_by_id['result'])) {

        // Preparing details
        $reply_id = $get_tweet_reply_by_id['result'][0]['tweet_reply_id'];
        $reply_hash = md5($get_tweet_reply_by_id['result'][0]['tweet_reply_message']);
        $reply_notify_status = $get_tweet_reply_by_id['result'][0]['tweet_reply_notification'];
        $chat_message = '@' . $get_tweet_reply_by_id['result'][0]['user_name'] . ' replied to a tweet - ' . site_url('tweet/' . $tweet_id . ' ' . urldecode('%0A%0A%0A'));

        // Checking notifiction already sent
        if ($reply_notify_status == 0) {

          // Getting notification email of owner
          if ($get_tweet_by_id['result']['user_notification'] == 1 && ($get_tweet_reply_by_id['result'][0]['user_id'] != $this->current_user['user_id'])) {
            if ($this->send_chat_to == 'WORK') {
              $email_ids[] = $get_tweet_by_id['result']['user_work_email'];
            } else {
              $email_ids[] = $get_tweet_by_id['result']['user_home_email'];
            }
          }

          // Getting notification email ids list of followers
          foreach ($get_followers['result'] as $tweet_followers) {
            if ($tweet_followers['user_notification'] == 1 && ($tweet_followers['user_id'] != $this->current_user['user_id'])) {
              if ($this->send_chat_to == 'WORK') {
                $email_ids[] = $tweet_followers['user_work_email'];
              } else {
                $email_ids[] = $tweet_followers['user_home_email'];
              }
            }
          }
        }

        // Setting return data
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $reply_hash;

        // Setting that notification sent for this tweet reply
        $this->tweet_model->upd_tweet_reply_notify_status($reply_id, 1);
      }
    }
    return $return_data;
  }

  private function for_new_tweet($tweet_id = NULL, $tweet_hash = NULL) {
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;
    $email_ids = array();
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($tweet_hash)) {
      $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);
      if (!empty($get_tweet_by_id['result'])) {
        $tweet_name = $this->sanitize(substr($get_tweet_by_id['result']['tweet_subject'], 0, 100));
        $tweet_user_id = $get_tweet_by_id['result']['user_id'];
        $tweet_user = '@' . $this->current_user['user_name'];
        $tweet_hash = md5($get_tweet_by_id['result']['tweet_message']);
        $tweet_notify = $get_tweet_by_id['result']['tweet_notification'];
        $chat_message = $tweet_user . ' added a new tweet  - ' . site_url('tweet/' . $tweet_id . ' ' . urldecode('%0A%0A%0A'));
        if (($tweet_user_id == $this->current_user['user_id']) && ($tweet_notify == 0)) {
          $get_all_users = $this->user_model->get_all_users(NULL, NULL);
          $users_data = $get_all_users['result'];
          if (!empty($users_data)) {
            foreach ($users_data as $user) {
              if ($user['user_notification'] == 1 && ($user['user_id'] != $this->current_user['user_id'])) {
                if ($this->send_chat_to == 'WORK') {
                  $email_ids[] = $user['user_work_email'];
                } else {
                  $email_ids[] = $user['user_home_email'];
                }
              }
            }
          }
        }
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $tweet_hash;
        $this->tweet_model->upd_tweet_notify_status($tweet_id, 1);
      }
    }
    return $return_data;
  }

  private function for_new_poll($poll_id = NULL, $poll_hash = NULL) {
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;
    $email_ids = array();
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_hash)) {
      $get_poll_by_id = $this->poll_model->get_poll_by_id($poll_id);
      if (!empty($get_poll_by_id['result'])) {
        $poll_name = $this->sanitize(substr($get_poll_by_id['result']['poll_message'], 0, 100));
        $poll_user_id = $get_poll_by_id['result']['user_id'];
        $poll_user = '@' . $this->current_user['user_name'];
        $poll_hash = md5($get_poll_by_id['result']['poll_message']);
        $poll_notify = $get_poll_by_id['result']['poll_notification'];
        $chat_message = $poll_user . ' added a new poll - ' . site_url('polls/view/' . $poll_id . ' ' . urldecode('%0A%0A%0A'));
        if (($poll_user_id == $this->current_user['user_id']) && ($poll_notify == 0)) {
          $get_all_users = $this->user_model->get_all_users(NULL, NULL);
          $users_data = $get_all_users['result'];
          if (!empty($users_data)) {
            foreach ($users_data as $user) {
              if ($user['user_notification'] == 1 && ($user['user_id'] != $this->current_user['user_id'])) {
                if ($this->send_chat_to == 'WORK') {
                  $email_ids[] = $user['user_work_email'];
                } else {
                  $email_ids[] = $user['user_home_email'];
                }
              }
            }
          }
        }
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $poll_hash;
        $this->poll_model->upd_poll_notify_status($poll_id, 1);
      }
    }
    return $return_data;
  }

  private function for_new_album($album_id = NULL, $album_hash = NULL) {
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;
    $email_ids = array();
    if (!empty($album_id) && is_numeric($album_id) && !empty($album_hash)) {
      $get_album_by_id = $this->album_model->get_album_by_id($album_id);
      if (!empty($get_album_by_id['result'])) {
        $album_name = $this->sanitize(substr($get_album_by_id['result']['album_name'], 0, 100));
        $album_user_id = $get_album_by_id['result']['album_user_id'];
        $album_user = '@' . $this->current_user['user_name'];
        $album_status = $get_album_by_id['result']['album_status'];
        $album_hash = md5($album_id . 'album');
        $album_notify = $get_album_by_id['result']['album_notification'];
        $chat_message = $album_user . ' added a new album ' . site_url('album/' . $album_id . ' ' . urldecode('%0A%0A%0A'));
        if (($album_user_id == $this->current_user['user_id']) && ($album_notify == 0) && ($album_status == 1)) {
          $get_all_users = $this->user_model->get_all_users(NULL, NULL);
          $users_data = $get_all_users['result'];
          if (!empty($users_data)) {
            foreach ($users_data as $user) {
              if ($user['user_notification'] == 1 && ($user['user_id'] != $this->current_user['user_id'])) {
                if ($this->send_chat_to == 'WORK') {
                  $email_ids[] = $user['user_work_email'];
                } else {
                  $email_ids[] = $user['user_home_email'];
                }
              }
            }
          }
        }
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $album_hash;
        $this->album_model->upd_album_notify_status($album_id, 1);
      }
    }
    return $return_data;
  }

  private function for_new_event($event_id = NULL, $event_hash = NULL) {
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;
    $email_ids = array();
    if (!empty($event_id) && is_numeric($event_id) && !empty($event_hash)) {
      $get_event_by_id = $this->event_model->get_event_by_id($event_id);
      if (!empty($get_event_by_id['result'])) {
        $event_name = $this->sanitize(substr($get_event_by_id['result']['event_message'], 0, 100));
        $event_user_id = $get_event_by_id['result']['event_user_id'];
        $event_user = '@' . $this->current_user['user_name'];
        $event_date = $get_event_by_id['result']['event_date'];
        $event_hash = md5($event_id . 'event');
        $event_notify = $get_event_by_id['result']['event_notification'];
        $chat_message = $event_user . ' added a new event "' . $event_name . '" on "' . date('d-m-Y', strtotime($event_date)) . '"';
        if (($event_user_id == $this->current_user['user_id']) && ($event_notify == 0)) {
          $get_all_users = $this->user_model->get_all_users(NULL, NULL);
          $users_data = $get_all_users['result'];
          if (!empty($users_data)) {
            foreach ($users_data as $user) {
              if ($user['user_notification'] == 1 && ($user['user_id'] != $this->current_user['user_id'])) {
                if ($this->send_chat_to == 'WORK') {
                  $email_ids[] = $user['user_work_email'];
                } else {
                  $email_ids[] = $user['user_home_email'];
                }
              }
            }
          }
        }
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $event_hash;
        $this->event_model->upd_event_notify_status($event_id, 1);
      }
    }
    return $return_data;
  }

  private function for_new_wall_post($wall_post_id = NULL, $wall_post_hash = NULL) {
    $return_data = array();
    $return_data['email_ids'] = array();
    $return_data['email_msg'] = NULL;
    $return_data['email_hash'] = NULL;
    $email_ids = array();
    if (!empty($wall_post_id) && is_numeric($wall_post_id) && !empty($wall_post_hash)) {
      $get_wall_post_by_id = $this->wall_model->get_wall_post_by_id($wall_post_id);
      if (!empty($get_wall_post_by_id['result'])) {
        $wall_post_user_id = $get_wall_post_by_id['result']['wall_post_user_id'];
        $wall_post_from_user_id = $get_wall_post_by_id['result']['wall_post_from_user_id'];
        $wall_post_user = '@' . $this->current_user['user_name'];
        $wall_post_hash = md5($wall_post_id . 'wall_post');
        $wall_post_notify = $get_wall_post_by_id['result']['event_notification'];
        $chat_message = $wall_post_user . ' posted on your wall';
        if (($wall_post_from_user_id == $this->current_user['user_id']) && ($wall_post_notify == 0)) {
          $get_user_by_id = $this->user_model->get_user_by_id($wall_post_user_id);
          $user_data = $get_user_by_id['result'];
          if (!empty($user_data)) {
            if ($user_data['user_notification'] == 1 && ($user_data['user_id'] != $this->current_user['user_id'])) {
              if ($this->send_chat_to == 'WORK') {
                $email_ids[] = $user_data['user_work_email'];
              } else {
                $email_ids[] = $user_data['user_home_email'];
              }
            }
          }
        }
        $return_data['email_ids'] = $email_ids;
        $return_data['email_msg'] = $chat_message;
        $return_data['email_hash'] = $wall_post_hash;
        $this->wall_model->upd_wall_post_notify_status($wall_post_id, 1);
      }
    }
    return $return_data;
  }

}

?>